package com.Dao;

import com.Dao.InterfaceMapper.OrderMapper;
import com.Dao.InterfaceMapper.RoleMapper;
import com.Dao.InterfaceMapper.StudentsMapper;
import com.Dao.InterfaceMapper.UserMapper;
import com.JavaBean.Ord_Rol_Use.Orders;
import com.JavaBean.Ord_Rol_Use.Role;
import com.JavaBean.Ord_Rol_Use.User;
import com.JavaBean.Stu_Sco_Cou_Tea.Students;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.log4j.PropertyConfigurator;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

public class MethodDao {
    // 链接
    private static SqlSession sqlSession;
    static {
        InputStream resourceAsStream = null;
        try {
            resourceAsStream = Resources.getResourceAsStream("mybatis_config.xml");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        //从配置文件中获取一个工厂对象

        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(resourceAsStream);

        //通过工厂获取sqlsession对象
        sqlSession = factory.openSession();

    }

    //日志
    public static void rizhi() throws IOException {
        Properties properties = new Properties();
        FileInputStream fileInputStream = new FileInputStream("src/main/resources/log4j.properties");
        properties.load(fileInputStream);
        PropertyConfigurator.configure(properties);
    }

    public static void selectonetoone() throws IOException {
        rizhi();
        OrderMapper mapper = sqlSession.getMapper(OrderMapper.class);
        ArrayList<Orders> selectonetoone = mapper.selectonetoone();
        for (Orders orders : selectonetoone) {
            System.out.println("orders = " + orders);
        }

    }

    public static void selectonetomore() throws IOException {
        rizhi();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        ArrayList<User> selectonetomore = mapper.selectonetomore();
        for (User user : selectonetomore) {
            System.out.println("user = " + user);
        }

    }
    public static void selectmoretomore() throws IOException {
        rizhi();
        RoleMapper mapper = sqlSession.getMapper(RoleMapper.class);
        ArrayList<Role> selectmoretomore = mapper.selectmoretomore();
        selectmoretomore.forEach(r -> System.out.println("role = " + r));


    }
    public static void selectmoretable() throws IOException {
        rizhi();
        StudentsMapper mapper = sqlSession.getMapper(StudentsMapper.class);
        List<Students> students = mapper.selectAllStudents();
        for (Students student : students) {
            System.out.println("student = " + student);
        }
    }
}
